using KinonekoSoftware.CodariaDB.Documents.Timelines;
namespace KinonekoSoftware.Studio.Pages.Projects
{
    public sealed class ProjectCompositionGalleryViewModel : RepositoryGalleryItem
    {
        private readonly TimeSystem  _engine_timeline;
        private          Composition _selected;

        public ProjectCompositionGalleryViewModel()
        {
            _engine_timeline = Platform.GetEngine<TimeSystem>();

            AddCompositionCommand    = CreateCommand(DoAddCompositionCommand);
            EditCompositionCommand   = CreateCommand<Composition>(DoEditCompositionCommand);
            UpdateCompositionCommand = CreateCommand<Composition>(DoUpdateCompositionCommand);
            RemoveCompositionCommand = CreateCommand<Composition>(DoRemoveCompositionCommand);

            Compositions = new ObservableCollection<Composition>();
        }

        protected override void OnStart()
        {
            Compositions.AddMany(_engine_timeline.GetCompositions(), true);
            base.OnStart();
        }


        private async void DoAddCompositionCommand()
        {
            var r = await MasterDetailViewModel.New(TitleSR.New, "新建一个作品集，作品集一般指的是某部小说、动画、漫画、游戏");

            if (!r.IsFinished)
            {
                return;
            }

            var composition = new Composition
            {
                Id    = ID.Get(),
                Name  = r.Value.Name,
                Intro = r.Value.Intro,
            };

            Compositions.Add(composition);
            _engine_timeline.Add(composition);
        }


        private void DoEditCompositionCommand(Composition composition)
        {
            
            if (composition is null)
            {
                return;
            }

            var context = new PageContext
            {
                Id = composition.Id,
            };
            
            context.Set(ContextBase.Key_Value, composition);
            GoTo<ProjectCompositionEditorViewModel>(context);
        }


        private async void DoUpdateCompositionCommand(Composition composition)
        {
            if (composition is null)
            {
                return;
            }
            
            var r = await MasterDetailViewModel.Edit(composition.Name, composition.Intro, TitleSR.New, "新建一个作品集，作品集一般指的是某部小说、动画、漫画、游戏");

            if (!r.IsFinished)
            {
                return;
            }

            composition.Name  = r.Value.Name;
            composition.Intro = r.Value.Intro;

            _engine_timeline.Update(composition);
        }


        private async void DoRemoveCompositionCommand(Composition composition)
        {
            if (composition is null)
            {
                return;
            }

            if (!await RemoveItem())
            {
                return;
            }

            Compositions.Remove(composition);
            _engine_timeline.Remove(composition);
        }


        
        public Composition Selected
        {
            get => _selected;
            set => SetValue(ref _selected, value);
        }
        
        public ICommandEX AddCompositionCommand    { get; }
        public ICommandEX EditCompositionCommand   { get; }
        public ICommandEX UpdateCompositionCommand { get; }
        public ICommandEX RemoveCompositionCommand { get; }

        public ObservableCollection<Composition> Compositions { get; }
    }
}